﻿using SecsEquip;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EquipSecs300
{
    public abstract class GEM_LoadPortAbstract : SecsEquipEx
    {
        internal int m_nPortNo = 0;
        OnCMSCarrierIDStatusChangeHandler m_pCMSCarrierIDStatusView;
        OnCMSCarrierSlotMapStatusChangeHandler m_pCMSCarrierSlotMapStatusView;
        OnCMSTransferStateChangeHandler m_pCMSTransferStateView;

        public PortTransferState_KE loadPortTransferState_KE;
        public CarrierIDStatus_KE carrierIDStatus_KE;
        public CarrierSlotMapStatus_KE carrierSlotMapStatus_KE;
        public string pCarrierID;
        public string CurSelCJobID { get; set; }
        public string CurSelPJobID { get; set; }
        public Dictionary<string, string> LotIDAndWaferID { get; set; } = new Dictionary<string, string>();
        internal GEM_LoadPort LoadPortBase { get; set; }
        public bool SamplesConfirm { get; set; }

        public GEM_LoadPortAbstract(GEM_LoadPort baseloadPort, SecsEquipHelper secsEquipHelper)
        {
            m_pName = "Carrier";
            this.LoadPortBase = baseloadPort;
            this.SecsEquipHelper = secsEquipHelper;
            //m_pCMSTransferStateView += OnCMSTransferStateChangeProcView;
            //m_pCMSCarrierIDStatusView += OnCmsCarrierIDStatusView;
            //m_pCMSCarrierSlotMapStatusView += OnCmsCarrierSlotMapStatusView;
        }

        internal string _CarrierID;
        public string CarrierID
        {
            get
            {
                return _CarrierID;
            }
        }
        internal List<ushort> _MapSlotResult;
        public List<ushort> MapSlotResult
        {
            get
            {
                return _MapSlotResult;
            }
        }

        // 设置端口号
        public void SetPortNo(int nPortNo)
        {
            m_nPortNo = nPortNo;
        }

        public int GetPortNo()
        {
            return m_nPortNo;
        }

        public override void UpdateData()
        {

        }

        /// <summary>
        /// 响应Load Port状态改变事件TransferStateChange
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="nPort"></param>
        /// <param name="nState"></param>
        public void OnCMSTransferStateChangeProc(object sender, int nPort, LoadPortTransferState nState)
        {
            loadPortTransferState_KE = (PortTransferState_KE)nState;

        }

        /// <summary>
        /// 响应状态改变CarrierIDStatus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="nPort"></param>
        /// <param name="pCarrierID"></param>
        /// <param name="nState"></param>
        public void OnCmsCarrierIDStatus(object sender, int nPort, string pCarrierID, CarrierIDStatus nState)
        {
            carrierIDStatus_KE = (CarrierIDStatus_KE)nState;
            if (carrierIDStatus_KE == CarrierIDStatus_KE.VERIFIED_OK)
            {
                if (nPort == 1)
                {
                    EquipSecs300.GlobalVar.SeAcceptCassette_PA = true;
                }
                else if (nPort == 2)
                {
                    EquipSecs300.GlobalVar.SeAcceptCassette_PB = true;
                }
            }
            else if (carrierIDStatus_KE == CarrierIDStatus_KE.VERIFY_FAILED)
            {
                if (nPort == 1)
                {
                    EquipSecs300.GlobalVar.SeRejectCassette_PA = true;
                }
                else if (nPort == 2)
                {
                    EquipSecs300.GlobalVar.SeRejectCassette_PB = true;
                }
            }
        }

        /// <summary>
        /// 响应状态改变SlotMapStatus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="nPort"></param>
        /// <param name="pCarrier"></param>
        public void OnCmsCarrierSlotMapStatus(object sender, int nPort, Carrier pCarrier)
        {
            //GetCarrierWaferInfo(pCarrier);

            if (CarrierSlotMapStatus.VERIFIED_OK == pCarrier.nSlotMapStatus
                && pCarrier.pCarrierID.Equals(pCarrierID))
            {
                carrierSlotMapStatus_KE = (CarrierSlotMapStatus_KE)CarrierSlotMapStatus.VERIFIED_OK;
            }
            else
            {
                carrierSlotMapStatus_KE = (CarrierSlotMapStatus_KE)pCarrier.nSlotMapStatus;
            }

            if (carrierSlotMapStatus_KE == CarrierSlotMapStatus_KE.VERIFIED_OK)
            {
                if (nPort == 1)
                {
                    EquipSecs300.GlobalVar.SeAcceptCassette_PA = true;
                }
                else if (nPort == 2)
                {
                    EquipSecs300.GlobalVar.SeAcceptCassette_PB = true;
                }
            }
            else if (carrierSlotMapStatus_KE == CarrierSlotMapStatus_KE.VERIFY_FAILED)
            {
                if (nPort == 1)
                {
                    EquipSecs300.GlobalVar.SeRejectCassette_PA = true;
                }
                else if (nPort == 2)
                {
                    EquipSecs300.GlobalVar.SeRejectCassette_PB = true;
                }
            }
        }

        //private void GetCarrierWaferInfo(Carrier pCarrier)
        //{
        //    LotIDAndWaferID.Clear();
        //    for (int i = 0; i < pCarrier.pSlotData.Length; i++)
        //    {
        //        if (!LotIDAndWaferID.ContainsKey(pCarrier.pSlotData[i].pSubstrateID))
        //        {
        //            LotIDAndWaferID.Add(pCarrier.pSlotData[i].pSubstrateID, pCarrier.pSlotData[i].pLotID);
        //        }
        //    }
        //}

        public string GetLotIDBysLotNO(int sLot)
        {
            string strLotID = "";// = LoadPortBase.CarrierInfo[m_nPortNo].pSlotData[sLot].pLotID;
            for (int i = 0; i < LoadPortBase.CarrierInfo[m_nPortNo].pSlotData.Length; i++)
            {
                if (LoadPortBase.CarrierInfo[m_nPortNo].pSlotData[i].nSlot == sLot)
                {
                    strLotID = LoadPortBase.CarrierInfo[m_nPortNo].pSlotData[i].pLotID;
                }
            }
            return strLotID;
        }
        public bool GetLotNOBySLotNO(List<bool> listBool, out string strLotID)
        {
            List<int> intList = UtilitiesConvert.ConvertBoolListToIntList(listBool);
            if (intList == null)
            {
                strLotID = "";
                return false;
            }
            strLotID = GetLotIDBysLotNO(intList[0]);
            return true;
        }

        public string GetWaferIDBySLotNO(int sLot)
        {
            string WaferID = "";
            for (int i = 0; i < LoadPortBase.CarrierInfo[m_nPortNo].pSlotData.Length; i++)
            {
                if (LoadPortBase.CarrierInfo[m_nPortNo].pSlotData[i].nSlot == sLot)
                {
                    WaferID = LoadPortBase.CarrierInfo[m_nPortNo].pSlotData[i].pSubstrateID;
                }
            }
            return WaferID;
        }

        // 其他内容处

    }
}
